﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BOHVote.Common;
using System.IO;
using System.Data.OleDb;
using System.Data;
using BOHVote.BLL;

namespace BOH.Exam
{
    public partial class TestResultImport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void butUpload_Click(object sender, EventArgs e)
        {
            try
            {
                if (fileExcel.PostedFile.ContentLength > 0)
                {
                    string fileFullName = fileExcel.PostedFile.FileName;
                    string FileName = System.IO.Path.GetFileName(fileExcel.PostedFile.FileName);
                    int intIndex =  fileFullName.IndexOf(".");
                    if (intIndex > 0)
                    {
                        string Namesuffix = System.IO.Path.GetExtension(fileFullName);
                        if (Namesuffix.ToLower() == ".xls" || Namesuffix.ToLower() == ".xlsx")
                        {
                            string strFullFolderPath = common.getAppSetting("FolderforSavePath") + "\\File";
                            string LoadFullPath = common.getAppSetting("FolderforLoadPath") + "/File";
                            common.CreateFolder(strFullFolderPath);
                            string FileFullPath = strFullFolderPath + "\\" + FileName;
                            if (File.Exists(FileFullPath))
                            {
                                File.Delete(FileFullPath);
                            }
                            fileExcel.PostedFile.SaveAs(FileFullPath);
                            butImportData.Enabled = true;
                            hideUploadFile.Value = FileFullPath;
                        }
                        else
                        {
                            labError.Text = "请选择正确格式的文件上传.";
                            butImportData.Enabled = false;
                            hideUploadFile.Value = string.Empty;
                        }
                    }
                    else
                    {
                        labError.Text = "请选择需要上传的文件.";
                        butImportData.Enabled = false;
                        hideUploadFile.Value = string.Empty;
                    }
                }
            }
            catch (Exception ex)
            {
                butImportData.Enabled = false;
            }
        }

        protected void butImportData_Click(object sender, EventArgs e)
        {
            try
            {
                if (!String.IsNullOrEmpty(hideUploadFile.Value.Trim()))
                {
                    string strExcelProvide = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + hideUploadFile.Value + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";
                    OleDbConnection objConnection = new OleDbConnection(strExcelProvide);
                    OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$]", objConnection);
                    OleDbDataAdapter objAdapter = new OleDbDataAdapter();
                    DataSet dsExcel = new DataSet();
                    objConnection.Open();

                    objAdapter.SelectCommand = objCmdSelect;
                    objAdapter.Fill(dsExcel);
                    if (dsExcel != null)
                    {
                        for (int i = 0; i < dsExcel.Tables[0].Rows.Count; i++)
                        {
                            labImportLog.Text = "正在处理第" +(i+1)+ "条数据......<br/>" + labImportLog.Text;
                            DataRow dr = dsExcel.Tables[0].Rows[i];
                            if (dr[1].ToString().Trim().Length > 0 && dr[2].ToString().Trim().Length > 0)
                            {
                                string strApplyID = ApplyClass.getApplyIDByIDCardAndPeriod(dr[1].ToString().Trim(), dr[2].ToString().Trim());
                                if (strApplyID.Trim().Length > 0)
                                {
                                    int j = 3;
                                    while (j < dsExcel.Tables[0].Columns.Count)
                                    {
                                        string strTestType = dr[j].ToString();
                                        string strScore = dr[j + 1].ToString();
                                        if (strScore.Trim().Length == 0)
                                            strScore = "0";
                                        if (ApplyTestClass.hasTestResult(strApplyID, dr[2].ToString().Trim(), strTestType))
                                            ApplyTestClass.UpdateTestResult(strApplyID, strTestType, strScore, dr[2].ToString().Trim());
                                        else
                                            ApplyTestClass.InsertNewTestResult(strApplyID, strTestType, strScore, dr[2].ToString().Trim());
                                        j = j + 2;
                                    }

                                    labImportLog.Text = "第" + (i + 1) + "条数据成功保存.<br/>" + labImportLog.Text;
                                }
                                else
                                {
                                    labImportLog.Text = "第" + (i + 1) + "条数据不存在，保存失败.<br/>" + labImportLog.Text;
                                }
                            }
                            else
                            {
                                labImportLog.Text = "第" + (i + 1) + "条数据不完整，保存失败.<br/>" + labImportLog.Text;
                            }
                        }
                    }
                    else
                    { 
                    }

                    objConnection.Close();
                }
            }
            catch (Exception ex)
            { 

            }
        }

    }
}
